package com.lyf.hdc.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lyf.hdc.api.result.Result;
import com.lyf.hdc.dto.*;
import com.lyf.hdc.entity.MachineFailure;
import com.lyf.hdc.mapper.MachineFailureMapper;
import com.lyf.hdc.service.MachineFailureService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalTime;
import java.util.List;

@Api(tags = "设备故障管理")
@ApiSupport(author = "lyf", order = 8)
@RestController
@RequestMapping("/machineFailure")
public class MachineFailureController {

    @Autowired
    private MachineFailureService machineFailureService;
    @Autowired
    private MachineFailureMapper machineFailureMapper;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "批量创建产线设备故障")
    @PostMapping("/batchCreate")
    public Result batchCreate(@RequestBody List<MachineFailureDto> machineFailureDtoList){
        return machineFailureService.batchCreate(machineFailureDtoList);
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "删除产线设备故障")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        return machineFailureService.delete(id);
    }
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "修改产线设备故障")
    @PostMapping("/update")
    public Result update(@RequestBody MachineFailureDto machineFailureDto){
        MachineFailure machineFailure = new MachineFailure();
        machineFailure.setId(machineFailureDto.getId());
        machineFailure.setGroupId(machineFailureDto.getGroupId());
        machineFailure.setMachineType(machineFailureDto.getMachineType());
        machineFailure.setFailureLevel(machineFailureDto.getFailureLevel());
        machineFailure.setFailureName(machineFailureDto.getFailureName());
        machineFailure.setMachineName(machineFailureDto.getMachineName());
        if(machineFailureDto.getFailureTimeStr() != null){
            machineFailure.setFailureTime(LocalTime.parse( machineFailureDto.getFailureTimeStr() ));
        }
        if(machineFailureDto.getFailureRecoverTimeStr() != null){
            machineFailure.setFailureRecoverTime(LocalTime.parse( machineFailureDto.getFailureRecoverTimeStr() ));
        }
        machineFailureMapper.updateById(machineFailure);
        return Result.success();
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "设备故障分页列表", notes = "用于设备故障分页列表查询使用")
    @PostMapping("/list")
    public Result<IPage<MachineFailureListDto>> list(@RequestBody MachineFailureFindDto request) {
        return machineFailureService.pageMachineFailures(request);
    }
}
